package com.by.yz.Register;

/**
 * Created by 你只能呵呵 on 2016/10/21.
 */

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.Response;
import com.android.volley.Request;
import com.android.volley.VolleyError;

import java.net.HttpCookie;
import java.util.List;
import java.util.Map;
import com.android.volley.AuthFailureError;
import com.by.yz.App;
import com.by.yz.loginUtil.Constants;

import java.util.HashMap;
import java.net.CookieStore;

public class DoctorREST {
    public interface MyGetCallBack {
        public void ok(JSONArray response) throws JSONException;

        public void error(String error);
    }

    public interface MyPostCallBack {
        public void ok(JSONObject response) throws JSONException;

        public void error(String error);
    }
    public void getDoctorRESTo(final MyGetCallBack cb) {


        String url = Constants.server_url + "/api/doctors";
        JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        try {
                            cb.ok(response);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                        cb.error(error.toString());
                    }
                }
        ) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                CookieStore cookieJar = App.cookieManager.getCookieStore();
                List<HttpCookie> cookies = cookieJar.getCookies();
                for (HttpCookie cookie : cookies) {
                    if (cookie.toString().contains("XSRF-TOKE")) {
                        Log.e("token-post", cookie.getValue());
                        headers.put("X-XSRF-TOKEN", cookie.getValue());
                    }
                }
                return headers;
            }
        };

        App.requestQueue.add(getRequest);
    }

    public void getDoctorRESTopage( int pag,final MyGetCallBack cb) {


        String url = Constants.server_url + "/api/doctors?page="+pag+"&size=5";
        JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        try {
                            cb.ok(response);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                        cb.error(error.toString());
                    }
                }
        ) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                CookieStore cookieJar = App.cookieManager.getCookieStore();
                List<HttpCookie> cookies = cookieJar.getCookies();
                for (HttpCookie cookie : cookies) {
                    if (cookie.toString().contains("XSRF-TOKE")) {
                        Log.e("token-post", cookie.getValue());
                        headers.put("X-XSRF-TOKEN", cookie.getValue());
                    }
                }
                return headers;
            }
        };

        App.requestQueue.add(getRequest);
    }

    public void postDoctorTestREST(JSONObject data, final MyPostCallBack cb) {

        String url = Constants.server_url + "/api/doctors";

        JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url, data,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            cb.ok(response);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                cb.error(error.toString());
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                CookieStore cookieJar = App.cookieManager.getCookieStore();
                List<HttpCookie> cookies = cookieJar.getCookies();
                for (HttpCookie cookie : cookies) {
                    if (cookie.toString().contains("XSRF-TOKE")) {
                        Log.e("token-post", cookie.getValue());
                        headers.put("X-XSRF-TOKEN", cookie.getValue());
                    }
                }

                headers.put("Content-Type", "application/json; charset=utf-8");
                return headers;
            }
        };
        App.requestQueue.add(postRequest);
    }

//
    public void putDoctorREST(JSONObject data, final MyPostCallBack cb) {

        String url = Constants.server_url + "/api/doctors";

        JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.PUT, url, data,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            cb.ok(response);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                cb.error(error.toString());
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                CookieStore cookieJar = App.cookieManager.getCookieStore();
                List<HttpCookie> cookies = cookieJar.getCookies();
                for (HttpCookie cookie : cookies) {
                    if (cookie.toString().contains("XSRF-TOKE")) {
                        Log.e("token-post", cookie.getValue());
                        headers.put("X-XSRF-TOKEN", cookie.getValue());
                    }
                }

                headers.put("Content-Type", "application/json; charset=utf-8");
                return headers;
            }
        };
        App.requestQueue.add(postRequest);
    }
//
//
//    public void delDoctorREST(int id, final MyPostCallBack cb) {
//
//        String url = Constants.server_url + "/api/car-infos/"+id;
//
//        JsonObjectRequest delRequest = new JsonObjectRequest(Request.Method.DELETE, url, null,
//                new Response.Listener<JSONObject>() {
//                    @Override
//                    public void onResponse(JSONObject response) {
//                        try {
//                            cb.ok(response);
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//
//                cb.error(error.toString());
//            }
//        }) {
//            @Override
//            public Map<String, String> getHeaders() throws AuthFailureError {
//                HashMap<String, String> headers = new HashMap<String, String>();
//                CookieStore cookieJar = App.cookieManager.getCookieStore();
//                List<HttpCookie> cookies = cookieJar.getCookies();
//                for (HttpCookie cookie : cookies) {
//                    if (cookie.toString().contains("CSRF-TOKE")) {
//                        Log.e("token-post", cookie.getValue());
//                        headers.put("X-CSRF-TOKEN", cookie.getValue());
//                    }
//                }
//
//                headers.put("Content-Type", "application/json; charset=utf-8");
//                return headers;
//            }
//        };
//        App.requestQueue.add(delRequest);
//    }
//
//    public void getOneDoctorREST(int id, final MyPostCallBack cb) {
//
//        String url = Constants.server_url + "/api/car-infos/"+id;
//
//        JsonObjectRequest delRequest = new JsonObjectRequest(Request.Method.GET, url, null,
//                new Response.Listener<JSONObject>() {
//                    @Override
//                    public void onResponse(JSONObject response) {
//                        try {
//                            cb.ok(response);
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//
//                cb.error(error.toString());
//            }
//        }) {
//            @Override
//            public Map<String, String> getHeaders() throws AuthFailureError {
//                HashMap<String, String> headers = new HashMap<String, String>();
//                CookieStore cookieJar = App.cookieManager.getCookieStore();
//                List<HttpCookie> cookies = cookieJar.getCookies();
//                for (HttpCookie cookie : cookies) {
//                    if (cookie.toString().contains("CSRF-TOKE")) {
//                        Log.e("token-post", cookie.getValue());
//                        headers.put("X-CSRF-TOKEN", cookie.getValue());
//                    }
//                }
//
//                headers.put("Content-Type", "application/json; charset=utf-8");
//                return headers;
//            }
//        };
//        App.requestQueue.add(delRequest);
//    }


}
